package montecarlo.gui;


import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import montecarlo.european.option.*;
import montecarlo.multi.thread.*;
import montecarlo.single.thread.*;

public class MonteCarloGui extends JFrame implements ActionListener{

	private String mode;
	private ArrayList <EuropeanOption> options;
	private JPanel pan;
	private JPanel pCenter;
	private JPanel pBas;
	private Box b1,b2,b3,b4;
	private JLabel lTitle, lTime,lRes, lCore, lThread;
	private JButton btnRun,btnQuit;
	private JComboBox combo;
	private final int NB_OPTIONS = 10;
	private String [] comboOptions = new String [NB_OPTIONS];
	
	public MonteCarloGui(ArrayList <EuropeanOption> options , String mode){
		this.options=options;
		this.mode=mode;
		initComponent();
	}
	
	private void initComponent(){
		  this.setTitle("GANZIN-COLLIGNON SII1 - Monte Carlo");
		  this.setSize(600, 700);
          this.setLocationRelativeTo(null);
          this.setDefaultCloseOperation(EXIT_ON_CLOSE);
          
          for(int i=0;i<options.size();i++){
        	 comboOptions[i] = options.get(i).toString();
          }
          
          /* Initialization */
          pan = new JPanel(new GridLayout(3,1));
          pCenter= new JPanel(new GridLayout(7,1));
          pBas=new JPanel();
          
          b1= Box.createHorizontalBox();
          b2= Box.createHorizontalBox();
          b3= Box.createHorizontalBox();
          b4= Box.createHorizontalBox();
          
          if(mode.equals("single"))lTitle=new JLabel("Monte Carlo - Single-threaded Version");
          else lTitle=new JLabel("Monte Carlo - Multi-threaded Version");
          lTitle.setHorizontalAlignment(SwingConstants.CENTER);
          lTitle.setForeground(Color.RED);
          
          lTime=new JLabel("Time (seconds) : ");
          lRes= new JLabel("Result : ");
          lCore=new JLabel();
          lThread=new JLabel();
          if(mode.equals("multi")){
        	  lCore.setText("Number of cores : ");
        	  lThread.setText("Number of threads : ");
          }
          combo = new JComboBox(comboOptions);
          combo.setSelectedIndex(0);
          
         
          
          btnRun = new JButton("Run");
          btnRun.addActionListener(this);
          btnQuit = new JButton("Quit");
          btnQuit.addActionListener(this);
          
          /* Add components to the frame */ 
          
          pan.add(lTitle);
          b1.add(combo);
          b1.add(Box.createHorizontalStrut(20));
          b1.add(btnRun);
          b3.add(lRes);
          b3.add(Box.createHorizontalStrut(200));
          b3.add(lTime);
          b4.add(lCore);
          b4.add(Box.createHorizontalStrut(200));
          b4.add(lThread);
          pCenter.add(b1);
          pCenter.add(new JLabel(""));
          pCenter.add(b2);
          pCenter.add(new JLabel(""));
          pCenter.add(b3);
          pCenter.add(new JLabel(""));
          pCenter.add(b4);
          pBas.add(btnQuit);
          pan.add(pCenter);
          pan.add(pBas);
          this.getContentPane().add(pan);
          
          
          pack();
          
          
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource() == btnRun){
			//If single thread mode
			double result;
			int i = combo.getSelectedIndex();
			EuropeanOption eo = options.get(i);
			if(mode.equals("single")){
				MonteCarloSingleThread mc = new MonteCarloSingleThread();	
				result= mc.MonteCarloStandardOption(eo.getCallputflag(),
						eo.getS(), eo.getX(), eo.getT(), eo.getR(), eo.getB(), eo.getV(),
						eo.getnSteps(), eo.getnSimulations());
				lRes.setText("Result : "+result);
				lTime.setText("Time (seconds) : "+mc.getTime());
			}
			//If multi thread
			else{
				MonteCarloMultiThread mc = new MonteCarloMultiThread(eo.getCallputflag(),
						eo.getS(), eo.getX(), eo.getT(), eo.getR(), eo.getB(), eo.getV(),
						eo.getnSteps(), eo.getnSimulations());
				                   
				lRes.setText("Result : "+mc.getRes());
				lTime.setText("Time (seconds) : "+mc.getTime());
				lCore.setText("Number of cores : "+mc.getNumberOfCores());
				lThread.setText("Number of threads : " + mc.getPoolSize());
			}
		}
		
		if(arg0.getSource() == btnQuit){
			System.exit(0);
		}
		
	}


	
	
}
